package com.peng.dao.impl;

import com.peng.dao.regionDao;
import com.peng.entity.region;
import com.peng.util.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by 欧阳欣鹏 on 2018/8/10.
 */
public class regionDaoImpl implements regionDao
{
    @Override
    public int findregionid(String areaName) {
        String sql = "SELECT areaId FROM region WHERE areaName= ?";
        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        int num = 0;
        con = BaseDao.getConnection();
        try {
            st = con.prepareStatement(sql);
            st.setString(1, areaName);
            rs = st.executeQuery();
            while (rs.next()) {
                num = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeConnection(con, st, rs);
        }
        return num;
    }

    @Override
    public region findregioname(int id) {
        region r = new region();
        String sql = "SELECT * FROM region WHERE areaId= ?";
        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        int num = 0;
        con = BaseDao.getConnection();
        try {
            st = con.prepareStatement(sql);
            st.setInt(1, id);
            rs = st.executeQuery();
            while (rs.next()) {
                r.setAreaId(rs.getInt("areaId"));
                r.setAreaCode(rs.getString("areaCode"));
                r.setLevel(rs.getInt("level"));
                r.setCityCode(rs.getString("cityCode"));
                r.setCenter(rs.getString("center"));
                r.setParentId(rs.getInt("parentId"));
                r.setAreaName(rs.getString("areaName"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeConnection(con, st, rs);
        }
        return r;
    }

    @Override
    public int findCity(String cityName) {
        int i = 0;
        String sql = "SELECT areaId FROM region WHERE areaName= ?";
        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        int num = 0;
        con = BaseDao.getConnection();
        try {
            st = con.prepareStatement(sql);
            st.setString(1, cityName);
            rs = st.executeQuery();
            while (rs.next()) {
                i = rs.getInt("areaId");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeConnection(con, st, rs);
        }
        return i;
    }


}
